package com.zxk.gulimall.product.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxk.common.utils.PageUtils;
import com.zxk.common.utils.Query;

import com.zxk.gulimall.product.dao.CategoryDao;
import com.zxk.gulimall.product.entity.CategoryEntity;
import com.zxk.gulimall.product.service.CategoryService;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listTreeCategory() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);

        List<CategoryEntity> categoryEntityList = categoryEntities.stream().filter(category -> category.getParentCid() == 0)
                .sorted((c1, c2) -> ((c1.getSort()==null?0:c1.getSort()) - (c2.getSort()==null?0:c2.getSort())))
                .map(category -> {
                    category.setChildren(getChildren(category,categoryEntities));
                    return category;
                })
                .collect(Collectors.toList());
        return categoryEntityList;
    }

    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();

        //递归查询是否还有父节点
        List<Long> parentPath = findParentPath(catelogId, paths);

        //进行一个逆序排列
        Collections.reverse(parentPath);

        return (Long[]) parentPath.toArray(new Long[parentPath.size()]);
    }

    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        paths.add(catelogId);
        CategoryEntity categoryEntity = baseMapper.selectById(catelogId);
        if(categoryEntity.getParentCid() != 0) {
            findParentPath(categoryEntity.getParentCid(),paths);
        }
        return paths;
    }

    private List<CategoryEntity> getChildren(CategoryEntity category, List<CategoryEntity> categoryEntities) {
        List<CategoryEntity> categoryEntityList = categoryEntities.stream()
                .filter(cat -> cat.getParentCid() == category.getCatId())
                .sorted((c1, c2) -> ((c1.getSort()==null?0:c1.getSort()) - (c2.getSort()==null?0:c2.getSort())))
                .map(cat -> {
                    cat.setChildren(getChildren(cat,categoryEntities));
                    return cat;
                }).collect(Collectors.toList());
        return categoryEntityList;
    }

}